package com.tilaiedu.mvc.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 1. 自定义一个异常处理器类，实现HandlerExceptionResolver接口
 * 2. 重写resolveException方法，对异常进行统一处理
 * 3. 在springmvc的配置文件中，配置异常处理器
 */
public class GlobalException implements HandlerExceptionResolver {
    // 执行这个方法，就说明当前项目有异常了
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler, Exception ex) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",ex.getMessage());
        //打印异常信息
        ex.printStackTrace();
        modelAndView.setViewName("500");
        return modelAndView;
    }
}